package qiyu.live.gift.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.qiyu.live.common.interfaces.enums.CommonStatusEnum;
import org.qiyu.live.common.interfaces.utils.ConvertBeanUtils;
import org.springframework.stereotype.Service;
import qiyu.live.gift.dto.GiftConfigDTO;
import qiyu.live.gift.provider.dao.mapper.GiftConfigMapper;
import qiyu.live.gift.provider.dao.po.GiftConfigPO;
import qiyu.live.gift.provider.service.IGiftConfig;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-10-23
 * Time: 20:30
 */
@Service
public class GiftConfigImpl implements IGiftConfig {

    @Resource
    private GiftConfigMapper giftConfigMapper;

    @Override
    public GiftConfigDTO getByGiftId(Integer giftId) {
        LambdaQueryWrapper<GiftConfigPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GiftConfigPO::getGiftId, giftId);
        queryWrapper.eq(GiftConfigPO::getStatus, CommonStatusEnum.VALID.getCode());
        queryWrapper.last("limit 1");
        GiftConfigPO giftConfigPO = giftConfigMapper.selectOne(queryWrapper);
        return ConvertBeanUtils.convert(giftConfigPO, GiftConfigDTO.class);
    }

    @Override
    public List<GiftConfigDTO> queryGiftList() {
        LambdaQueryWrapper<GiftConfigPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GiftConfigPO::getStatus, CommonStatusEnum.VALID.getCode());
        List<GiftConfigPO> giftConfigPOS = giftConfigMapper.selectList(queryWrapper);
        return ConvertBeanUtils.convertList(giftConfigPOS, GiftConfigDTO.class);
    }

    @Override
    public void insertOne(GiftConfigDTO giftConfigDTO) {
        giftConfigMapper.insert(ConvertBeanUtils.convert(giftConfigDTO, GiftConfigPO.class));
    }

    @Override
    public void updateOne(GiftConfigDTO giftConfigDTO) {
        giftConfigMapper.updateById(ConvertBeanUtils.convert(giftConfigDTO, GiftConfigPO.class));
    }
}